﻿//-----------------------------------------------------------------
//
// @(#)$Id: EbaySelling.aspx.cs,v 1.0 2013/12/13 17:54:53 lichunhua Exp $
// @(#)$Author: lichunhua  $
// @(#)$Date: 2013/12/13 17:54:53 $
// @(#)$Description:
//
//
//
//                 All Rights Reserved.
//-----------------------------------------------------------------
using System;
using System.Data;
using System.Web.UI.WebControls;
using ebay.Dal;
using ebay.Dal.CenterDB_EbayTableAdapters;
using eBay.Service.Core.Soap;

public partial class UI_Orders_EbaySelling : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        T_EbayAccountTableAdapter ebayAccountAdapter = new T_EbayAccountTableAdapter();
        ddlebayUserId.DataSource = ebayAccountAdapter.GetData();
        ddlebayUserId.DataTextField = "EbayUserId";
        ddlebayUserId.DataValueField = "EbayUserId";
        ddlebayUserId.DataBind();
        ddlebayUserId.Items.Insert(0, new ListItem("《--请选择--》", " "));
        string[] statusCol = Enum.GetNames(typeof(CompleteStatusCodeType));
        foreach (string code in statusCol)
        {
            if (code != "CustomCode")
            {
                ddlStatus.Items.Add(code);
            }
        }
        ddlStatus.Items.Insert(0, new ListItem("All", "All"));
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        WebUtility.CheckRight();
        if (!IsPostBack)
        {
            beginDate.Value = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
            endDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
        }
    }

    public void Export()
    {
        string ebayUserId = ddlebayUserId.Value == null ? "" : ddlebayUserId.Value;
        string fromDate = beginDate.Value;
        string toDate = endDate.Value;
        string Where = "";
        string orderId = txtOrderId.Value;
        string orderStatus = ddlStatus.Value;
        if (!string.IsNullOrEmpty(fromDate) && !string.IsNullOrEmpty(toDate))
        {
            fromDate = WebUtility.InputText(fromDate, 12);
            toDate = WebUtility.InputText(toDate, 12);
            DateTime TimeFrom = DateTime.Parse(fromDate);
            DateTime TimeTo = DateTime.Parse(toDate);      
            if (Where.Length > 0)
                Where += " And";
            Where += " CONVERT (Varchar(8), EbaySelling.ImportDate, 112) >= '" + TimeFrom.ToString("yyyyMMdd") + "' AND CONVERT (Varchar(8), EbaySelling.ImportDate, 112) <= '" + TimeTo.ToString("yyyyMMdd") + "'";
        }
        if (!string.IsNullOrEmpty(ebayUserId.Trim()))
        {
            ebayUserId = WebUtility.InputText(ebayUserId, 80);

            if (Where.Length > 0)
                Where += " And";
            Where += " EbaySelling.EbayUserId = '" + ebayUserId + "'";
        }        
        if (!string.IsNullOrEmpty(orderId))
        {
            if (Where.Length > 0)
                Where += " And";
            Where += " EbaySelling.OrderId = '" + orderId + "'";
        }
        if (!string.IsNullOrEmpty(orderStatus) && orderStatus != "All")
        {
            if (Where.Length > 0)
                Where += " And";
            Where += " EbaySelling.CompleteStatus = '" + orderStatus + "'";
        }
        string cmdText = @"Select EbayUserId,OrderLineItemId,OrderId,OrderSalesRecordNumber,SalesRecordNumber,UserId,BuyerFullName,BuyerEmail,BuyerAddress1,BuyerAddress2,
            BuyerCity,BuyerState,BuyerPostcode,BuyerCountry,ItemId,TransactionID,ItemTitle,Quantity,SalePrice,FinalValueFee,IncludedVATRate,PostageAndPackaging,Insurance,TotalPrice,
            PaymentMethod,PayPalTransactionID,InvoiceNumber,InvoiceDate,SaleDate,CheckoutDate,PaidonDate,DispatchDate,PostageService,FeedbackLeft,FeedbackReceived,NotestoYourself,
            CustomLable,PrivateNotes,ProductIDType,ProductIDValue,ProductIDValue2,VariationSKU,Variation_Name,Variation_Value,VariationTitle,VariationViewItemURL,eBayPaymentStatus,
            BuyerSelectedShipping,FeedbackScore,PositiveFeedbackPercent,ExternalTransactionTime,FeeOrCreditAmount,PaymentOrRefundAmount,Platform,CompleteStatus,CheckoutStatus ,LastTimeModified FROM EbaySelling Where " + Where;

        DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.ceterDB_Conn, CommandType.Text, cmdText,null);
        ds.Tables[0].TableName = "EbaySelling";
        DataSetToExcel.DataSetOutExcel(ds.Tables[0], "销售订单");
    }

    protected void btnExport_Click(object sender, EventArgs e)
    {
        Export();
    }
}
